package game

import (
	"github.com/hwcer/cosgo"
	"github.com/hwcer/cosgo/times"
	"github.com/hwcer/logger"
	"server/game/model"
)

func init() {
	cosgo.On(cosgo.EventTypLoaded, serverLoader)
}

func serverLoader() error {
	today := times.Daily(-180)
	expire := today.Now().Unix()
	//清理邮件
	logger.Trace("开始清理过期邮件")
	tx := model.DB().Model(&model.Mail{}).Where("expire < ?", expire)
	if err := tx.Delete().Error; err != nil {
		logger.Trace("清理过期邮件错误:%v", err)
	} else if tx.RowsAffected > 0 {
		logger.Trace("清理过期邮件完成，累计删除:%v", tx.RowsAffected)
	}
	//清理日常打点
	logger.Trace("开始清理日常数据")
	s, _ := today.Sign(0)
	tx = model.DB().Model(&model.Daily{}).Where("iid < ?", s)
	if err := tx.Delete().Error; err != nil {
		logger.Trace("清理日常数据错误:%v", err)
	} else if tx.RowsAffected > 0 {
		logger.Trace("清理日常数据完成，累计删除:%v", tx.RowsAffected)
	}
	//清理活动
	logger.Trace("开始清理活动数据")
	tx = model.DB().Model(&model.Active{}).Where("expire > ? AND expire < ?", 0, expire)
	if err := tx.Delete().Error; err != nil {
		logger.Trace("清理日常数据错误:%v", err)
	} else if tx.RowsAffected > 0 {
		logger.Trace("清理日常数据完成，累计删除:%v", tx.RowsAffected)
	}

	return nil
}
